package com.bazaaroid.mobile;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MapItemOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	
	public MapItemOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}
	
	public MapItemOverlay(Drawable defaultMarker, Context context){
		super(boundCenterBottom(defaultMarker));
			
		mContext = context;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	/*
	 * Needed in order to add new OverlayItems to the ArrayList
	 */
	public void addOverlay(OverlayItem overlay){
		mOverlays.add(overlay);
		// call the populate() for the ItemizedOverlay that will read each of the OverlayItems and prepare them to be drown.
		populate();
	}
	
	/*
	 * Overriding the onTap(int) callback method, which will handle the event when an item is tapped by the user.
	 */
	@Override
	protected boolean onTap(int index)
	{
		  OverlayItem item = mOverlays.get(index);
		  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		  dialog.setTitle(item.getTitle());
		  dialog.setMessage(item.getSnippet());
		  dialog.show();
		  return true;	
	}
}
